package base.world;

import java.util.ArrayList;
import java.util.Random;

import com.living.Block;

public class WorldGen
{
	protected static Random rand = new Random();

	public static Block[] generateBox(int par1, int par2, int par3, int par5)
	{
		Block[] blockList = new Block[par3 * par5];
		for(int i = 0; i < par3; i++)
		{
			for (int j = 0; j < par5; j++)
			{
				blockList[(i * par5) + j] = new Block(par1 + (i * 16), par2 + (j * 16));
			}
		}

		return blockList;
	}

	/**Clear blocks from spawn area
	 * @param par1Block - list of Block
	 * @param par1 - height of row
	 * @param par2 - x blocks
	 * @param par3 - y blocks
	 * @param par5 - radius
	 * @return
	 */
	public static Block[] clearSpawn(Block[] par1Block, int par2, int par3, int par4, int par5)
	{
		int blockid = (par3 * par2) + par4;
		if (blockid > 0 && blockid < par1Block.length && par1Block[blockid] != null)
		{
			par1Block[blockid].setDead();
			par1Block[blockid] = null;
		}

		for(int i = -par5; i < par5; i++)
		{
			for (int j = -par5; j < par5; j++)
			{
				int blockid1 = blockid + (i * par2) + j;
				if (blockid1 > 0 && blockid1 < par1Block.length && par1Block[blockid1] != null)
				{
					par1Block[blockid1].setDead();
					par1Block[blockid1] = null;
				}
			}
		}

		return par1Block;
	}

	/**Generate blocks as world type : Scatter
	 * @param par1Block - list of Block
	 * @param par2 - percent of blocks in world
	 * @return
	 */
	public static Block[] generateScatter(Block[] par1Block, float par2)
	{
		ArrayList<Block> blocklist = new ArrayList<Block>();

		for (Block block : par1Block)
		{
			blocklist.add(block);
		}

		while(blocklist.size() > par1Block.length * par2)
		{
			int blockid = rand.nextInt(blocklist.size());
			Block block = blocklist.get(blockid);

			if (block != null)
			{
				block.setDead();
				par1Block[blockid] = null;
				blocklist.remove(block);
			}
		}

		return par1Block;
	}

	/**Generate blocks as world type : AntColony
	 * @param par1Block - list of Block
	 * @param par2 - height of row
	 * @param par3 - percent of blocks in world
	 * @return list of remaining blocks
	 */
	public static Block[] generateAntColony(Block[] par1Block, int par2, float par3)
	{
		ArrayList<Block> blocklist = new ArrayList<Block>();

		for (Block block : par1Block)
		{
			blocklist.add(block);
		}

		while(blocklist.size() > par1Block.length * par3)
		{
			int blockid = rand.nextInt(blocklist.size());
			Block block = blocklist.get(blockid);

			if (block != null)
			{
				block.setDead();
				par1Block[blockid] = null;
				blocklist.remove(block);

				int endPath = 0;
				int clearPath = 0;

				while (endPath == 0)
				{
					Block[] blocks = {null, null, null, null};

					if (blockid + par2 < par1Block.length)
					{
						//Get East block
						blocks[0] = par1Block[blockid + par2];
					}

					if (blockid - 1 > 0)
					{
						//Get North block
						blocks[1] = par1Block[blockid - 1];
					}

					if (blockid - par2 > 0)
					{
						//Get West block
						blocks[2] = par1Block[blockid - par2];
					}

					if (blockid + 1 < par1Block.length)
					{
						//Get South block
						blocks[3] = par1Block[blockid+1];
					}

					clearPath = 4;
					endPath = 1;

					if (blocks[0] != null || blocks[1] != null || blocks[2] != null || blocks[3] != null)
					{
						while (clearPath > 0)
						{
							int i = rand.nextInt(4);
							Block block1 = blocks[i];

							if (block1 != null)
							{
								blockid += i == 0 ? 1 : i == 1 ? -par2 : i == 2 ? -1 : par2;
								block1.setDead();
								par1Block[blockid] = null;
								blocklist.remove(block1);

								clearPath = 0;
								endPath = 0;	
							}
							else
							{
								clearPath = -1;
							}
						}
					}
				}
			}
		}

		return par1Block;
	}

	/**Generate blocks as world type : UnderGroundLab
	 * @param par1Block - list of Block
	 * @param par2 - height of row
	 * @param par3 - percent of blocks in world
	 * @return list of remaining blocks
	 */
	public static Block[] generateUnderGroundLab(Block[] par1Block, int par2, float par3)
	{
		ArrayList<Block> blocklist = new ArrayList<Block>();

		for (Block block : par1Block)
		{
			blocklist.add(block);
		}

		while(blocklist.size() > par1Block.length * par3)
		{
			int blockid = rand.nextInt(blocklist.size());
			Block block = blocklist.get(blockid);

			if (block != null)
			{
				block.setDead();
				par1Block[blockid] = null;
				blocklist.remove(block);

				int endPath = 0;
				int prevDir = rand.nextInt(4);

				while (endPath == 0)
				{
					Block[] blocks = {null, null, null, null};

					if (blockid + par2 < par1Block.length)
					{
						//Get East block
						blocks[0] = par1Block[blockid + par2];
					}

					if (blockid - 1 > 0)
					{
						//Get North block
						blocks[1] = par1Block[blockid - 1];
					}

					if (blockid - par2 > 0)
					{
						//Get West block
						blocks[2] = par1Block[blockid - par2];
					}

					if (blockid + 1 < par1Block.length)
					{
						//Get South block
						blocks[3] = par1Block[blockid+1];
					}

					int lookAround = 1;
					endPath = 1;

					if (blocks[0] != null || blocks[1] != null || blocks[2] != null || blocks[3] != null)
					{
						while (lookAround == 1)
						{
							int i = rand.nextInt(6);
							i = i == 4 || i == 5 ? prevDir : i;

							Block block1 = blocks[i];

							if (block1 != null)
							{
								blockid += i == 0 ? 1 : i == 1 ? -par2 : i == 2 ? -1 : par2;
								block1.setDead();
								par1Block[blockid] = null;
								blocklist.remove(block1);

								lookAround = 0;
								endPath = 0;	
							}
						}
					}
				}
			}
		}

		return par1Block;
	}

	/**Generate blocks as world type : ChipBoard
	 * @param par1Block - list of Block
	 * @param par2 - height of row
	 * @param par3 - percent of blocks in world
	 * @return list of remaining blocks
	 */
	public static Block[] generateChipBoard(Block[] par1Block, int par2, float par3)
	{
		ArrayList<Block> blocklist = new ArrayList<Block>();

		for (Block block : par1Block)
		{
			blocklist.add(block);
		}

		while(blocklist.size() > par1Block.length * par3)
		{
			int blockid = rand.nextInt(blocklist.size());
			Block block = blocklist.get(blockid);

			if (block != null)
			{
				block.setDead();
				par1Block[blockid] = null;
				blocklist.remove(block);

				int endPath = 0;

				while (endPath == 0)
				{
					Block[] blocks = {null, null, null, null};

					if (blockid + (par2 * 2) < par1Block.length)
					{
						//Get Far East block
						blocks[0] = par1Block[blockid + (par2 * 2)];
					}

					if (blockid - (1 * 2) > 0)
					{
						//Get Far North block
						blocks[1] = par1Block[blockid - (1 * 2)];
					}

					if (blockid - (par2 * 2) > 0)
					{
						//Get Far West block
						blocks[2] = par1Block[blockid - (par2 * 2)];
					}

					if (blockid + (1 * 2) < par1Block.length)
					{
						//Get Far South block
						blocks[3] = par1Block[blockid + (1 * 2)];
					}

					endPath = 1;

					for (int i = 0; i < 4; i++)
					{
						if (blocks[i] != null && (rand.nextBoolean() || i == 3))
						{
							if (i == 0)
							{
								Block block1 = par1Block[blockid + par2];
								if (block1 != null)
								{
									block1.setDead();
									par1Block[blockid + par2] = null;
									blocklist.remove(block1);

									blockid += par2 * 2;
									blocks[i].setDead();
									par1Block[blockid] = null;
									blocklist.remove(blocks[i]);
								}
							}
							else if (i == 1)
							{
								Block block1 = par1Block[blockid - 1];
								if (block1 != null)
								{
									block1.setDead();
									par1Block[blockid - 1] = null;
									blocklist.remove(block1);

									blockid -= 1 * 2;
									blocks[i].setDead();
									par1Block[blockid] = null;
									blocklist.remove(blocks[i]);
								}
							}
							else if (i == 2)
							{
								Block block1 = par1Block[blockid - par2];
								if (block1 != null)
								{
									block1.setDead();
									par1Block[blockid - par2] = null;
									blocklist.remove(block1);

									blockid -= par2 * 2;
									blocks[i].setDead();
									par1Block[blockid] = null;
									blocklist.remove(blocks[i]);
								}
							}
							else if (i == 3)
							{
								Block block1 = par1Block[blockid + 1];
								if (block1 != null)
								{
									block1.setDead();
									par1Block[blockid + 1] = null;
									blocklist.remove(block1);

									blockid += 1 * 2;
									blocks[i].setDead();
									par1Block[blockid] = null;
									blocklist.remove(blocks[i]);
								}
							}

							break;
						}
					}
				}
			}
		}

		return par1Block;
	}
}
